﻿#include "widget.h"
#include "breakdialog.h"
#include "config.h"
#include "ui_widget.h"
#include <QAction>
#include <QCloseEvent>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QMenu>
#include <QMessageBox>
#include <QTimer>
#include <Windows.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    dialogSetting=new DialogSetting(this);

    updateConfig();

    createActions();
    createTrayIcon();
    createTimer();
    setWindowIcon(icon);

    connect(trayIcon, &QSystemTrayIcon::activated, this, &Widget::iconActivated);
    connect(timer, &QTimer::timeout, this, &Widget::updateOneSec);
    connect(dialogSetting, &DialogSetting::sendUpdateConfig, this, &Widget::updateConfig);

    trayIcon->show();
    timer->start(INTERVAL);

    bell = new QSound(":/assets/info.wav");
}

Widget::~Widget()
{
    delete ui;
    delete timer;
    delete restoreAction;
    delete quitAction;
    delete setAction;
    delete trayIcon;
    delete trayIconMenu;
    delete dialogSetting;
}

void Widget::closeEvent(QCloseEvent *event)
{
    trayIcon->setVisible(true);
    this->hide();
    event->ignore();
    qDebug()<<"隐藏窗口";
}

void Widget::createActions()
{
    QIcon iconRestore=QIcon(":/assets/icons/show.png");
    restoreAction = new QAction("显示", this);
    restoreAction->setIcon(iconRestore);
    connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);

    QIcon iconQuit=QIcon(":/assets/icons/exit.png");
    quitAction = new QAction("退出", this);    
    quitAction->setIcon(iconQuit);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);

    QIcon iconSetting=QIcon(":/assets/icons/setting.png");
    setAction = new QAction("设置", this);
    setAction->setIcon(iconSetting);
    connect(setAction, &QAction::triggered, dialogSetting, &QDialog::exec );
}

void Widget::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(quitAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(setAction);
    trayIconMenu->addAction(restoreAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);

    trayIcon->setIcon(icon);
    setWindowIcon(icon);

    trayIcon->setVisible(true);
}

void Widget::createTimer()
{
    timer=new QTimer(this);
}

bool Widget::takeBreak()
{
    BreakDialog breakDialog(this, breakSteps);
    if(breakDialog.exec()==QDialog::Accepted)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void Widget::takeBreakHard()
{

    BreakDialog breakDialog(this, breakSteps);
    breakDialog.setPostoff(false);
    breakDialog.exec();
}

void Widget::waitForCursorMove()
{
    Sleep(1000);
    QPoint pre_pt = QCursor::pos();
    Sleep(1000);
    QPoint cur_pt = QCursor::pos();
    while(pre_pt==cur_pt)
    {
        Sleep(2000);
        cur_pt = QCursor::pos();
        qDebug()<<"鼠标静止";
    }
    qDebug()<<"鼠标移动";

}

void Widget::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
        case QSystemTrayIcon::Trigger:
        case QSystemTrayIcon::DoubleClick:
            show();
            break;
        default:
            break;
    }
}

void Widget::updateOneSec()
{
    static int curSec=0;
    timer->stop();
    QDateTime time = QDateTime::currentDateTime();
    QString strTime = time.toString("yyyy-MM-dd hh:mm:ss");
    ui->labelTime->setText(strTime);
    curSec=(curSec+1)%60;
    qDebug()<<curSec<<curStep;
    if(curSec==0)
        curStep=curStep+1;
    if(curStep<workSteps)
    {
        ui->progressBar->setValue(curStep);
        ui->label->setText(QString::asprintf("%d/%d", curStep, workSteps));
    } else {

        qDebug() << "时间到";
        bell->play();

        bool ret = takeBreak();
        if(ret==true)
        {
            qDebug()<<"正常休息";
            curStep=0;
            workSteps = workStepsCopy;
            bell->play();
            Sleep(3000);
            waitForCursorMove();

        }
        else
        {
            if(curPostoff<postoffMax)
            {
                qDebug()<<"延时休息";
                curStep=0;
                curPostoff+=1;
                workSteps = postoffSteps;

                ui->progressBar->setMaximum(workSteps);
                ui->label->setText(
                    QString::asprintf("%d/%d", curStep, workSteps));
            }
            else
            {
                qDebug()<<"强制休息";
                takeBreakHard();

                curPostoff = 0;
                curStep=0;
                workSteps = workStepsCopy;

                ui->progressBar->setMaximum(workSteps);
                ui->label->setText(
                    QString::asprintf("%d/%d", curStep, workSteps));
                bell->play();
                Sleep(3000);
                waitForCursorMove();
            }
        }
    }
    timer->start(INTERVAL);
}

void Widget::updateConfig()
{
    QJsonValue jsonValue;
    curStep=0;
    curPostoff=0;
    workSteps=config.getConfig(QString("workSteps"));
    workStepsCopy=config.getConfig(QString("workSteps"));
    breakSteps=config.getConfig(QString("breakSteps"));
    postoffMax=config.getConfig(QString("postoffMax"));

    ui->progressBar->setMaximum(workSteps);
    ui->label->setText(QString::asprintf("%d/%d", curStep, workSteps));
}


